import 'package:environmental_les_app/common/utils/sp_util.dart';
import 'package:flutter/services.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';
import 'package:flutter_clx_base/utils/permission_helper.dart';
import 'package:flutter_clx_base/widget/normal_dialog.dart';
import 'package:flutter_z_location/flutter_z_location.dart';

import '../widgets/select_bottom_dialog.dart';
import 'coordinate_converter.dart';

/// 调起拨号页
void launchTelURL(String phone) async {
  final String url = 'tel:$phone';
  launchUrl(Uri.parse(url)).then((value) {
    if (!value) {
      ToastUtil.showToast('拨号失败！');
    }
  });
}

/// 拨打客服电话
void callServerPhone() {
  Get.dialog(NormalDialog(
      title: '拨打电话',
      content: "呼叫客服：4008350677",
      onConfirm: () {
        launchTelURL("4008350677");
      }));
}

/// actionBar-客服电话widget
callServerWidget() {
  return InkWell(
    onTap: callServerPhone,
    child: Padding(
      padding: const EdgeInsets.only(right: 15.0),
      child:
          ImageWidget.loadAssetImage("icon_server", width: 28.0, height: 28.0),
    ),
  );
}

/// 拨打电话
void callPhone({String? content, String? phone}) {
  Get.dialog(NormalDialog(
      title: '拨打电话',
      content: content,
      onConfirm: () {
        launchTelURL(phone ?? "");
      }));
}

Widget headWidget({String? url, double? width, double? height}) {
  return url.isNullOrEmpty()
      ? ClipOval(
          child:
              ImageWidget.loadAssetImage("head", width: width, height: height),
        )
      : ImageWidget.loadNetImage(
          url,
          width: width,
          height: height,
          maxWidth: width != null ? 3 * width.toInt() : null,
          maxHeight: height != null ? 3 * height.toInt() : null,
          type: ImageTransformerType.circle,
        );
}

/// 拼接url
String appendUrl(String? url) {
  return !url.isNullOrEmpty() && url!.startsWith("http")
      ? url
      : "${getDomain() ?? ""}${url ?? ""}";
}

requestPermissionCamera({
  VoidCallback? onGranted,
  VoidCallback? onRefuse,
  VoidCallback? onSettings,
  VoidCallback? onCancelSettings,
}) {
  requestPermission(
    permissionList: [Permission.camera],
    toSettingTip: "请开启相机权限",
    onGranted: () {
      // 是否定位，
      onGranted?.call(); // 授权权限回调
    },
    onRefuse: onRefuse,
    onSettings: onSettings,
    onCancelSettings: onCancelSettings,
  );
}

requestPermissionPhotos({
  VoidCallback? onGranted,
  VoidCallback? onRefuse,
  VoidCallback? onSettings,
  VoidCallback? onCancelSettings,
}) {
  requestPermission(
    permissionList: [Permission.photos, Permission.photosAddOnly],
    toSettingTip: "请开启相册权限",
    onGranted: () {
      // 是否定位，
      onGranted?.call(); // 授权权限回调
    },
    onRefuse: onRefuse,
    onSettings: onSettings,
    onCancelSettings: onCancelSettings,
  );
}

///查看是否有定位权限
Future<bool> checkPermissionLocation() async {
  return await PermissionHelper.checkPermission([Permission.location]);
}

///查看是否有相机权限
Future<bool> checkPermissionCamera() async {
  return await PermissionHelper.checkPermission([Permission.camera]);
}

/// 查看是否有相册权限
Future<bool> checkPermissionPhotos() async {
  return await PermissionHelper.checkPermission(
      [Permission.photosAddOnly, Permission.photos]);
}

/// 申请位置权限，原生方式获取经纬度
requestPermissionLocation({
  VoidCallback? onGranted,
  VoidCallback? onRefuse,
  VoidCallback? onSettings,
  VoidCallback? onCancelSettings,
}) {
  requestPermission(
    onGranted: () async {
      final coordinate = await FlutterZLocation.getCoordinate();
      logger.e("经度：${coordinate.longitude},纬度：${coordinate.latitude}");

      final gcj02 =
          GpsToGcj02.gps84ToGcj02(coordinate.latitude, coordinate.longitude);
      logger.e("高德经度：${gcj02[1]},高德纬度：${gcj02[0]}");
    },
    permissionList: [Permission.location],
    toSettingTip: "请开启位置权限",
  );
}

// 复制到剪贴板
void onCopy(String? str, String tips) {
  Clipboard.setData(ClipboardData(text: str ?? ''));
  ToastUtil.showToast(tips);
}

// 选择呼叫或复制

selectCallorCopy(String phone) {
  Get.bottomSheet(SelectBottomDialog(
      select1: "呼叫",
      callback1: () {
        launchTelURL(phone);
      },
      select2: "复制号码",
      callback2: () {
        onCopy(phone, "号码已复制");
      }));
}

// 正则规则
final RegExp phoneRegExp = RegExp(r'^\d{11}$');
final RegExp emailRegExp =
    RegExp(r'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}$');
